<?php

namespace App\Admin\Extensions;

use Encore\Admin\Admin;

class CollectorState
{
    protected $id = '';
    protected $collector_id;
    protected $orderid;

    public function __construct($orderid, $verName)
    {
        $this->orderid = $orderid;
        $this->verName = $verName;
    }

    protected function script()
    {
        return <<<SCRIPT
 layui.use('layer', function(){
        var layer = layui.layer;
        $('.collector_sys').click(function () {
            var that = this;
            var orderid = $(that).attr('data-orderid');
            var user_name = $(that).attr('data-name');
           
             var index = layer.open({
                content: '/admin/collectorsys/weiwai',
                type: 2,
                title: '<\span style="color:red">'+user_name+'</\span>',
                area: ['300px', '210px'],
                btn: ['确定', '取消'],
                yes: function(index, layero){
                    var repayment_state = $(layero).find("iframe").contents().find("#setSchool").val();
                    var cuishou_mon = 0;
                    if(repayment_state == 4){
                        cuishou_mon = $(layero).find("iframe").contents().find(".mon").val();
                        if(cuishou_mon === ''){
                            layer.msg('催收金额不能为空');
                            return;
                        }
                    }
                    $.ajax({
                        url: '/admin/collectorsys?orderid='+orderid+'&repayment_state='+repayment_state+'&cuishou_mon='+cuishou_mon,
                        type: 'get',
                        success: function (res) {
                            if(res.code) {
                                layer.msg(res.text)
                                layer.close(index);
                                return;
                            }
                            layer.msg(res.text)
                            layer.close(index);
                            location.href = location.href
                        }
                    })
                }
            });
        });
 });

    
SCRIPT;
    }

    protected function render()
    {
        Admin::script($this->script());

        return '&nbsp;<button data-orderid="' . $this->orderid . '" data-name="' . $this->verName . '" type="button" class="collector_sys layui-btn-normal layui-btn layui-btn-xs"><i class="fa fa-refresh" aria-hidden="true"></i></button>';
    }

    public function __toString()
    {
        return $this->render();
    }
}
